<template>
	<view class="content" @tap.stop="$refs.uniselect.close()">
        <u-navbar bgColor="#fff" :fixed="true" :safeAreaInsetTop="true" :placeholder="true"
            @leftClick="back" title="预警" rightIcon="search" @rightClick="rightClick">
        </u-navbar>
        <uni-select class="uniselect" 
            ref="uniselect" bgColor="#fff" :selectData="selectData" @select="select">
        </uni-select>
        <view style="height:74rpx"></view>
   
        <!-- 内容 -->
        <view class="uni-area">
            <view class="u-m-t-30 u-font-32 font-bold">预警情况统计</view>
            <view class="u-m-t-30 uni-shadow eleBgColor">
                <u-row>
                    <u-col v-for="(item,index) in warnings" :key="index"
                        :class="[index==0?'border':'']"
                        span="6" style="flex-direction:row; justify-content:center">
                        <u--image :src="item.icon" width="80rpx" height="80rpx" shape="circle"></u--image>
                        <view class="u-m-l-20 u-felx u-flex-col">
                            <text class="u-font-42 font-bold">{{item.value}}</text>
                            <text class="u-m-t-6 u-font-24 uni-dec-color">{{item.title}}</text>
                        </view>
                    </u-col>
                </u-row>
            </view>
            <view class="u-m-t-30 uni-shadow u-p-l-10 u-p-t-10 eleBgColor">
                <view class="u-p-t-100" style="height:620rpx">
                    <qiun-data-charts type="pie" 
                        :opts="{legend:{position:'bottom',float:'left',lineHeight:20}}" 
                        :chartData="warningEcharts" />
                </view>
            </view>

            <!-- 预警列表 -->
            <view class="u-m-t-40">
                <view class="u-flex u-row-between">
                    <text class="u-font-30 font-bold">预警列表</text>
                    <view class="u-flex colony" @tap="warn.show=true">
                        <text class="u-m-r-16 u-font-26">{{warn.label}}</text>
                        <u-icon name="arrow-down" size="13" color="#5D6672"></u-icon>
                    </view>
                </view>
                <uni-item-warn class="u-m-t-30" v-for="(tem,ind) in page.list" :key="ind" :listItem="tem" />
            </view>

            <!-- 返回顶部 -->
            <u-back-top :scroll-top="page.scrollTop" @tap="toTop"></u-back-top>
            <!-- 加载更多 -->
            <u-loadmore :status="page.status" />
        </view>

        <!-- 预警筛选 -->
        <u-picker :show="warn.show" :columns="warn.data" keyName="label" :closeOnClickOverlay="true"
            @cancel="warn.show=false" @close="warn.show=false" @confirm="selectWarn">
        </u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
                // 下拉选择数据
				selectData:[
                    { label: "属地", list: [], show: false, type: "districtId", value: '' },
                    { label: "项目类型", list: [], show: false, type: "xmtype", value: '' },
                ],
                // 筛选参数
                prameter:{
                    districtId: '', // 区划值【非必传】
                    xmtype:'', // 项目类型【非必传】 
                },
                // 预警情况统计
                warnings:[
                    { icon:'/static/icons/warn1.png',title:'本月预警', value: 0 },
                    { icon:'/static/icons/warn2.png',title:'累计预警', value: 0 },
                ],
                warningEcharts:{
                    series: [
                        // {
                        //     data: [
                        //         // { name: "住建", value: 50 }
                        //     ],
                        //     format: 'totalWarn', // 数据格式化
                        // }
                    ],
                },
                // 属地筛选
                warn:{
                    show: false,
                    label: '本月预警',
                    value: 1,
                    data: [
                        [
                            { label: '本月预警', value: 1},
                            { label: '历史预警', value: 2},
                        ]
                    ]
                },
                // 分页相关参数
                paging:{
                    pageNum: 1,
                    pageSize: this.$config.pageSize,
                },
                page:{
                    scrollTop: 0,
                    status: 'nomore',
                    total: 0,
                    list: [], // 列表
                },
			}
		},


        onPageScroll(e) {
            this.page.scrollTop = e.scrollTop;
        },
        onReachBottom(){
            if( this.page.list.length < this.page.total ){
                this.paging.pageNum += 1
                this.page.status = 'loading'
                this.getWarningList()
            }else{
                this.page.status = 'nomore'
            }
        },

		onLoad() {
            // 属地
            this.$tools.getNationFn(this,{},res=>{
                this.selectData.splice(0,1,res)
                this.prameter.districtId = res.value
            })
            // 项目类型
            this.$tools.getProTypeFn(this,res=>{
                this.selectData.splice(1,1,res)
                this.prameter.xmtype = res.value
            })
            // 预警情况统计
            this.getWarningTotal()
            // 预警图表
            this.getWarningEcharts()
            // 预警列表
            this.getWarningList()
		},

		methods: {
            back(){
                uni.navigateBack()
            },
            rightClick(){
                uni.navigateTo({ url: '/pages/common/search?type=warn' })
            },


            // 下拉选择
            select(e){
                this.prameter[e.type] = e.value
                // 预警情况统计
                this.getWarningTotal()
                // 预警图表
                this.getWarningEcharts()
                // 预警列表
                this.paging.pageNum = 1
                this.page.list = []
                this.toTop()
                this.getWarningList()
            },

            // 预警情况统计
            getWarningTotal(){
                this.$request(this, this.$apis.warning,this.prameter).then(res => {
                    // console.log('预警情况统计',res)
                    this.warnings[0].value = res.mvalue
                    this.warnings[1].value = res.value
                }).then()
            },
            // 预警图表
            getWarningEcharts(){
                uni.showLoading({ title:'预警' })
                this.$request(this, this.$apis.warningEcharts,this.prameter).then(res => {
                    // console.log('预警',res)
                    this.warningEcharts.series = [ { data: [], format:'totalWarn' } ]
                    res.map((v,i)=>{
                        this.warningEcharts.series[0].data.push({
                            name: v.name,
                            value: v.value
                        })
                    })
                }).then()
            },
            // 预警列表
            getWarningList(){
                uni.showLoading({ title:'预警列表加载中' })
                const prameter = Object.assign(this.prameter,this.paging)
                prameter.type = this.warn.value
                // console.log('预警列表参数',prameter)
                this.$request(this,this.$apis.warningList,prameter).then(res=>{
                    // console.log('预警列表',res)
                    this.page.total = res.total
                    if( res.total && res.total != 0 ){
                        this.status = 'loadmore'
                        res.data.map((v,i)=>{
                            this.page.list.push(v)
                        })
                    }else{
                        this.page.status = 'nomore'
                        uni.$u.toast('暂未查询到更多预警相关数据')
                    }
                }).then()
            },


            // 选择本月预警&历史预警
            selectWarn(e){
                this.warn.label = e.value[0].label
                this.warn.value = e.value[0].value
                this.warn.show = false
                // 预警列表
                this.paging.pageNum = 1
                this.page.list = []
                this.toTop()
                this.getWarningList()
            },

            // 回到顶部
            toTop(){
                uni.pageScrollTo({ scrollTop: 0, duration: 300 })
            }
		}
	}
</script>

<style scoped lang="scss">
    .content{
        .uniselect{
            width: 100%;
            position: fixed !important;
            left: 0;
        }
        .border{
            border-right: 1px solid #eee;
        }
        .eleBgColor{
            background: $uni-text-color-inverse;
            position: relative;
            .unit{
                position: absolute;
                top: 36rpx;
                right: 30rpx;
            }
        }
    }
</style>
